package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.MallPaymentDtoMapper;
import com.starlink.model.domain.MallPaymentDO;
import com.starlink.mapper.MallPaymentMapper;
import com.starlink.model.request.MallPaymentRequest;
import com.starlink.service.MallPaymentService;
import com.starlink.model.util.MallPaymentConvert;
import com.starlink.model.vo.MallPaymentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class MallPaymentServiceImpl extends ServiceImpl<MallPaymentMapper, MallPaymentDO> implements MallPaymentService {
    @Autowired
    private MallPaymentMapper mallPaymentMapper;

    @Override
    public MallPaymentVO getByKey(long id) {
        MallPaymentVO mallPaymentVO = MallPaymentDtoMapper.INSTANCE.do2VO(getById(id));
        return mallPaymentVO;
    }

    @Override
    public PageInfo<MallPaymentVO> pageQuery(Pageable pageable,MallPaymentRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<MallPaymentDO> list = mallPaymentMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallPaymentVO> mallPaymentVOList = MallPaymentConvert.buildMallPaymentVoList(list);
        return new PageInfo(mallPaymentVOList);
    }

    @Override
    public List<MallPaymentVO> listQuery(MallPaymentRequest request) {
        List<MallPaymentDO> list = mallPaymentMapper.selectDOList(MallPaymentDtoMapper.INSTANCE.request2DO(request));
        List<MallPaymentVO> mallPaymentVOS = MallPaymentConvert.buildMallPaymentVoList(list);
        return mallPaymentVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallPaymentRequest request) {
        request.setId(getId());
        save(MallPaymentDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallPaymentRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallPaymentDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        mallPaymentMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public String selectWxOrderNumByOrderId(Long orderId) {
        return mallPaymentMapper.selectWxOrderNumByOrderId(orderId);
    }
}
